<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="exampleModalLabel">发放通话券</h4>
        </div>
        <form style="padding:4%;height:300px;overflow-y:auto;" id="vouchersForm" role="form">
            <div class="form-group">
                <label class="control-label">金额:</label>
                <input type="text" class="form-control" name="money" value="" required>
                <div class="help-block with-errors"></div>
            </div>
            <div class="form-group">
                <label class="control-label">有效期:</label>
                <div class="input-group date" id="period_date">
                    <input type='text' value="" name="period_date" class="form-control " data-date-format="yyyy-mm-dd" />
                    <div class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </div>
                </div>
            </div>
            <input type="hidden" name="user_id" value="{$userId}" />
        </form>
        <div class="modal-footer">
            <button type="button" data-dismiss="modal" class="btn btn-default">取消</button>
            <button type="button" id="vouchersSave" class="btn btn-primary">保存</button>
        </div>
    </div>
</div>
<script type="text/javascript">

    $(function () {
        $("#period_date").datetimepicker({
            format: "yyyy-mm-dd",
            language: "ch",
            pickDate: true,
            pickTime: false,
            autoclose: true,
            minView: 2
        }).on('hide',function(e) {
            var bv = $('#vouchersForm').data('bootstrapValidator');
            if(bv){
                bv.updateStatus('period_date', 'NOT_VALIDATED',null).validateField('period_date');
            }
        });

        $("#vouchersSave").click(function () {
            if(!validateForm()){
                return false;
            }
            $.ajax({
                url: "{:U('Admin/User/doVouchers')}",
                type: "post",
                dataType: "json",
                data: $("#vouchersForm").serialize(),
                success: function (data) {
                    if(data.status){
                        $("#commonModal").modal('toggle');
                    }
                    common.alert("提示", data.msg);
                }
            });
        });
    });

    function validateForm(){
        $('#vouchersForm').bootstrapValidator({
            feedbackIcons: {
                valid: 'glyphicon glyphicon-ok',
                //invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                money: {
                    validators: {
                        notEmpty: {
                            message: '通话券金额不能为空'
                        },
                        regexp: {
                            regexp: /((^[1-9]+[0-9]*)|^0)(\.\d{0,2})?$/,
                            message: '通话券金额必须为数字,且最多2位小数'
                        }
                    }
                },
                period_date: {
                    validators: {
                        notEmpty: {
                            message: '有效日期不能为空'
                        }
                    }
                },
            }
        });
        var data = $('#vouchersForm').data('bootstrapValidator');
        if (data) {
            data.validate();
            if (!data.isValid()) {
                return false;
            }
        }
        return true;
    }


</script>